package cn.marinda.runnable;

import cn.marinda.MinecraftServerHelper;
import cn.marinda.entity.Server;
import cn.marinda.entity.ServerInfo;
import cn.marinda.util.*;

import javax.swing.*;

/**
 * 下载任务线程
 *
 * @author admin
 * @date 2022/07/11
 */
public class DownloadServerCoreRunnable implements Runnable{

    private JProgressBar progressBar;
    private ServerInfo serverInfo = MinecraftServerHelper.serverInfo;

    /**
     * 下载可运行
     *
     * @param progressBar 进度条
     */
    public DownloadServerCoreRunnable(JProgressBar progressBar){
        this.progressBar = progressBar;
    }

    @Override
    public void run() {
        String url = HttpUtils.urlPath;
        if(url == null || url == ""){
            return;
        }
        HttpUtils.downloadFlag = HttpUtils.downloadFile(progressBar);
        if(HttpUtils.downloadFlag){
            HttpUtils.serverEnum = null;
            HttpUtils.urlPath = null;
            if(!ServerUtils.downloadCoreFlag){
                FileUtils.moveServerConfig();
            }
            if(HttpUtils.isOnlineCore) {
                SwingUtils.downloadDialog.close();
            }
            ThreadUtils.downloadScheduled.cancel(true);
        }
    }

}
